<?php

namespace app\common\model\mysql;

use think\Model;

class Order extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'or_order';

    public function getNormalOrderList($where,$limit,$page,$field){
        $order = [
            "order_id" =>"desc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据where条件获取订单数据
     * @param array $where
     * @param string $field
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderFind(array $where = [], string $field = "*"){
        $result = $this->where($where)->field($field)->find();
        return $result ? $result->toArray() : [];
    }

    /**
     * 获取订单单个字段数组
     * @param $where
     * @param $field
     * @return array
     */
    public function getNormalOrderFiledArr($where, $field){
        $order = [
            "order_id" =>"asc",
        ];
        $result = $this->where($where)->order($order)->column($field);
        return $result;
    }

    /**
     * 根据主键id更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function saveOrderData($id,$data){
        $id = intval($id);
        if(empty($id) || empty($data) || !is_array($data)){
            return false;
        }
        $where = [
            "order_id"=>$id,
        ];
        return $this->where($where)->save($data);
    }

    /**
     * 根据$where条件更新数据表中的数据
     * @param $id
     * @param $data
     * @return bool
     */
    public function saveOrderWhereData($where,$data){
        if(empty($data) || !is_array($data)){
            return false;
        }
        return $this->where($where)->save($data);
    }

    /**
     * 订单表数量加X
     * @param $id
     * @param $field
     * @param int $count
     * @return false
     */
    public function saveIncOrderField($id, $field, int $count = 1){
        $id = intval($id);
        if(empty($id) || empty($field)){
            return false;
        }
        $where = [
            "order_id"=>$id,
        ];
        return $this->where($where)->inc($field,$count)->update();
    }

    /**
     * 获取订单表$field最大值
     * @param $where
     * @param $field
     * @return mixed
     */
    public function getNormalOrderMax($where,$field){
        return $this->where($where)->max($field);
    }

    /**
     * 新增订单
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public function addOrderData($data){
        $this->save($data);
        return $this->id;
    }
}
